import os
import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory
# 加载其他python格式的launch文件
from launch.launch_description_sources import PythonLaunchDescriptionSource


def generate_launch_description():
    # 获取与拼接默认路径
    autopatrol_robot_path = get_package_share_directory('autopatrol_robot')
    # 推荐的写法
    default_patrol_config_path = os.path.join(autopatrol_robot_path, 'config', 'patrol_config.yaml')

    # # 获取其他包的路径
    fishbot_description_path = get_package_share_directory('fishbot_description')
    # fishbot_navigation2_path = get_package_share_directory('fishbot_navigation2')
    # # 定义要包含的launch文件路径
    gazebo_sim_launch = os.path.join(fishbot_description_path, 'launch', 'gazebo_sim.launch.py')
    # navigation2_launch_path = os.path.join(fishbot_navigation2_path, 'launch', 'navigation2.launch.py')
    
    # 包含其他launch文件
    include_gazebo = launch.actions.IncludeLaunchDescription(
        PythonLaunchDescriptionSource(gazebo_sim_launch),
    )
    # include_navigation2 = launch.actions.IncludeLaunchDescription(
    #     PythonLaunchDescriptionSource(navigation2_launch_path),
    # )
    

    action_patrol_node = launch_ros.actions.Node(
        package='autopatrol_robot',
        executable='patrol_node',
        output='screen',
        parameters=[default_patrol_config_path],
    )

    action_speaker_node = launch_ros.actions.Node(
        package='autopatrol_robot',
        executable='speaker',
        output='screen',
    )

    return launch.LaunchDescription([
        # # 先启动gazebo和导航，再启动自定义节点（顺序可调整）
        include_gazebo,
        # include_navigation2,


        # '''下面的代码启动方式错误,OnProcessExit 监听的是 “进程退出”，而非 “启动完成”'''
        # '''对于gazebo和rviz2都是长期运行的节点,不存在退出一说'''
        # # 事件动作，当加载机器人结束后执行    
        # launch.actions.RegisterEventHandler(
        #     event_handler=launch.event_handlers.OnProcessExit(
        #         target_action=include_gazebo,
        #         on_exit=[include_navigation2],)
        # ),
        # # 事件动作，load_fishbot_diff_drive_controller
        # launch.actions.RegisterEventHandler(
        # event_handler=launch.event_handlers.OnProcessExit(
        #     target_action=include_navigation2,
        #     on_exit=[action_patrol_node],)
        # ),
        action_patrol_node,
        action_speaker_node,
    ])

    